package com.mvc.base.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.extremecomponents.table.limit.Limit;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.mvc.base.model.BaseGroup;
import com.mvc.base.service.BaseCompanyManager;
import com.mvc.framework.dao.PropertyFilter;
import com.mvc.framework.model.BaseEntity;
import com.mvc.framework.util.FrameworkContextUtils;
import com.mvc.framework.util.PageSearch;
import com.mvc.framework.util.PageUtils;
import com.mvc.framework.web.PageController;

public abstract class BaseGroupController<T extends BaseGroup> extends PageController<T> {

	@RequestMapping("my")
	public String my(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Limit limit = PageUtils.getLimit(request);
		PageSearch page = preparePage(request, limit);
		page.getFilters().add(new PropertyFilter("EQI_userId", FrameworkContextUtils.getCurrentUserId() + ""));
		String result = handlePage(request, page);
		afterPage(request, page, IS_NOT_BACK);
		request.setAttribute("action", "my");
		return result;
	}

	@RequestMapping("select")
	public String select(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Limit limit = PageUtils.getLimit(request);
		PageSearch page = preparePage(request, limit);
		page.getFilters().add(new PropertyFilter("EQI_status", BaseGroup.STATUS_VALID + ""));
		handlePage(request, page);
		afterPage(request, page, IS_NOT_BACK);
		request.setAttribute("action", "select");
		return getFileBasePath() + "select" + getActualArgumentType().getSimpleName();
	}

	@RequestMapping("changeStatus/{id}/{status}")
	public String changeStatus(ModelMap modelMap, @PathVariable Integer id, @PathVariable Integer status) throws Exception {
		modelMap.addAttribute("result", false);
		if (id != null & status != null) {
			T baseGroup = getEntityManager().get(id);
			if (baseGroup.getStatus() == null || !baseGroup.getStatus().equals(status)) {
				baseGroup.setStatus(status);
				getEntityManager().save(baseGroup);
				modelMap.addAttribute("result", true);
			}
		}
		return "jsonView";
	}

	protected PageSearch preparePage(HttpServletRequest request, Limit limit) {
		request.setAttribute("action", "page");
		PageSearch pageSearch = super.preparePage(request, limit);
		return pageSearch;
	}

	protected String handleSave(HttpServletRequest request, ModelMap modelMap, T t) throws Exception {
		if (t.getUserId() == null) {
			t.setUserId(FrameworkContextUtils.getCurrentUserId());
		}
		getEntityManager().save(t);
		return "redirect:edit/" + ((BaseEntity) t).getId() + getMessage("common.base.success", request) + "&action="
		        + request.getParameter("action");
	}

	@RequestMapping("get/{objectId}")
	public String get(ModelMap modelMap, @PathVariable Integer objectId) throws Exception {
		modelMap.addAttribute("entity", ((BaseCompanyManager) getEntityManager()).getByObjectId(objectId));
		return "jsonView";
	}

}
